import {
	OrbitControls
} from '@/common/controls/OrbitControls.js'

export function test(canvas, THREE) {
	var camera, scene, renderer;
	var mesh;
	init();
	animate();

	function init() {
		camera = new THREE.PerspectiveCamera(75, canvas.width / canvas.height, 0.25, 1000);
		camera.position.set(0, 100, 400)
		scene = new THREE.Scene();
		// 创建背景
		scene.background = new THREE.Color("rgb(176, 226, 255)");
		scene.fog = new THREE.Fog(scene.background, 1, 5000);

		// 创建地面
		var light = new THREE.HemisphereLight(0xffffff, 0x444444);
		light.position.set(0, 20, 0);
		scene.add(light);
		light = new THREE.DirectionalLight(0xffffff);
		light.position.set(0, 20, 10);
		scene.add(light);
		// ground
		mesh = new THREE.Mesh(new THREE.PlaneBufferGeometry(1000, 1000), new THREE.MeshPhongMaterial({
			color: 0x999999,
			depthWrite: false
		}));
		mesh.rotation.x = -Math.PI / 2;
		scene.add(mesh);


		// 创建第一个盒子
		var geometry = new THREE.BoxGeometry(50, 50, 50);
		var material = new THREE.MeshBasicMaterial({
			color: "rgb(250, 255, 205)"
		});
		mesh = new THREE.Mesh(geometry, material);

		scene.add(mesh);
		renderer = new THREE.WebGLRenderer({
			antialias: true
		});
		renderer.setPixelRatio(wx.getSystemInfoSync().pixelRatio);
		renderer.setSize(canvas.width, canvas.height);

		// 模型控制
		const controls = new OrbitControls(camera, renderer.domElement);
		controls.update();
	}

	function onWindowResize() {
		camera.aspect = window.innerWidth / window.innerHeight;
		camera.updateProjectionMatrix();
		renderer.setSize(canvas.width, canvas.height);
	}

	function animate() {
		canvas.requestAnimationFrame(animate);
		renderer.render(scene, camera);
		// console.log('有没有运行这个')
	}


	function addModel(position) {
		console.log('新增模型方法')
		var addgeometry = new THREE.BoxGeometry(50, 50, 50);
		// 创建天地墙材质
		var materials = [
			new THREE.MeshBasicMaterial({
				color: 0xff0000
			}), // 右
			new THREE.MeshBasicMaterial({
				color: 0x00ff00
			}), // 左
			new THREE.MeshBasicMaterial({
				color: 0x0000ff
			}), // 上
			new THREE.MeshBasicMaterial({
				color: 0xffff00
			}), // 下
			new THREE.MeshBasicMaterial({
				color: 0xff00ff
			}), // 后
			new THREE.MeshBasicMaterial({
				color: 0x00ffff
			}) // 前
		];
		mesh = new THREE.Mesh(addgeometry, materials);
		const {
			x,
			y,
			z
		} = position
		console.log('x,y,z',
			x, y, z)
		mesh.position.set(x, y, z)
		scene.add(mesh);

	}



	return {
		addModel,
		camera,
		scene
	}
}